package com.xia.controller;

import com.xia.dao.article.ILikeArticleDao;
import com.xia.domian.Article;
import com.xia.domian.ReplyComment;
import com.xia.service.IArticleService;
import com.xia.util.annotation.GainPageView;
import com.xia.util.common.Result;
import com.xia.dao.article.ICommentDao;
import com.xia.domian.Comment;
import com.xia.service.ICommentService;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 评论控制器
 */
@CrossOrigin
@RestController
@RequestMapping("/comment")
public class CommentController {
    private ICommentService commentService;
    private ICommentDao commentDao;
    private IArticleService articleService;
    private ILikeArticleDao likeArticleDao;

    @Autowired
    public void setLikeArticleDao(ILikeArticleDao likeArticleDao) {
        this.likeArticleDao = likeArticleDao;
    }
    @Autowired
    public void setArticleService(IArticleService articleService) {
        this.articleService = articleService;
    }
    @Autowired
    public void setCommentService(ICommentService commentService) {
        this.commentService = commentService;
    }
    @Autowired
    public void setCommentDao(ICommentDao commentDao) {
        this.commentDao = commentDao;
    }

    /**
     * 删除评论
     */
    @DeleteMapping("/deleteComment/{commentId}")
    public Result deleteComment(@PathVariable Integer commentId){
        commentDao.deleteComment(commentId);
        return Result.success();
    }

    /**
     * 查看其他用户对自己作品进行评论的消息
     */
    @GetMapping("/selectReplyOneselfComment/{usersId}")
    public Result selectReplyOneselfComment(@PathVariable Integer usersId){
        return Result.success(commentService.selectReplyOneselfCommentServer(usersId));
    }

    /**
     * 用户进行评论
     */
    @PostMapping("/insertComment")
    public Result insertComment(@NotNull @RequestBody Comment comment) throws IOException {
        return Result.success(commentService.userComment(comment));
    }

    /**
     * 用户查看详细文章并将用户对文章浏览记录下来
     */
    @GainPageView
    @GetMapping("/selectArticleAndComment")
    public Result selectComment(@RequestParam Integer articleId,@RequestParam String usersIP,@RequestParam String usersId){
        Map<String,Object> map= new HashMap<>();
        List<Article> multipleArticleList=articleService.selectArticleSizeUtil(0,articleId,0,0);
        Article multipleArticle=multipleArticleList.get(0);
        if (!usersId.equals("undefined")){
            Integer isLikeState=likeArticleDao.selectUsersLikeArticle(articleId,Integer.valueOf(usersId));
            multipleArticle.setLike(isLikeState != null);
        }else {
            multipleArticle.setLike(false);
        }
        Map<String,Object> commentMap=commentService.selectArticleComment(articleId,1);
        if (commentMap.containsKey("error")){
            return Result.error("此评论数量不够");
        }
        map.put("multipleArticle",multipleArticle);
        map.put("commentMap",commentMap);
        return Result.success(map);
    }

    /**
     * 查看用户评论和回复的消息
     */
    @GetMapping("/selectUsersComment/{usersId}")
    public Result selectUsersComment(@PathVariable Integer usersId){
        Result result =Result.success();
        List<ReplyComment> replyData= commentDao.selectReply(0,usersId);
        result.put("replyData",replyData);
        List<Comment> commentData= commentDao.selectComment(0,usersId,1);
        result.put("commentData",commentData);
        return result;
    }
}
